package com.rocksea.app.weight;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathEffect;
import android.graphics.Rect;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.rocksea.app.R;
import com.rocksea.app.db.lowStrain.LowStrainDb;

import java.util.ArrayList;

/**
 * 低应变波形展示视图 extends RecyclerView
 * 注意：与之一同使用的布局文件名为：item_layout_low_strain_db.xml
 * 用于展示一个低应变数据下所有波形图
 * <p/>
 * <p/>
 * #####须实现的方法：
 * 1.void setLowStrainDb(LowStrainDb lowStrainDb, boolean isShowValid)
 * 用途：设置低应变数据，用来展示 isShowValid为true表示展示的是有效数据，否则展示的是无效数据
 * <p/>
 * <p/>
 * #####可选实现的方法
 * 1.void setOnItemValidChangedListener(OnItemValidChangedListener listener)
 * 用途：某个数据有效性改变时的监听事件
 * <p/>
 * 2.void setRecycleViewPattern(byte pattern)
 * 用途：设置视图的显示模式，传入LowStrainDbView.PATTERN_NORMAL表示正常模式
 * 传入LowStrainDbView.PATTERN_EDIT表示编辑模式
 * 注意：因为涉及到适配器所以，该方法调用之前须调用setLowStrainDb方法设置数据
 * <p/>
 * 3.byte getRecycleViewPattern()
 * 用途：获取当前视图显示的模式
 * 返回值两种：LowStrainDbView.PATTERN_NORMAL  和 LowStrainDbView.PATTERN_EDIT
 * <p/>
 * <p/>
 */
public class LowStrainDbView extends RecyclerView {
    protected LowStrainDb mLowStrainDb;//用来绘制低应变数据对象
    protected ArrayList<LowStrainDb.LowStrainDataDb> mLowStrainDataDbList = null;//用来显示的集合
    protected Context mContext;
    private OnItemValidChangedListener mListener;
    private LinearLayoutManager mLayoutManager;
    private LowStrainDbAdapter mAdapter;
    private Bitmap changeValidBitmap = null;
    private boolean isShowValid = true;

    //显示模式
    public static final byte PATTERN_NORMAL = 0;//正常模式
    public static final byte PATTERN_EDIT = 1;//编辑模式
    private byte CURRENT_PATTERN = PATTERN_NORMAL;//当前模式  默认为正常

    public LowStrainDbView(Context context) {
        super(context);
        mContext = context;
    }

    public LowStrainDbView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
    }

    public LowStrainDbView(Context context, @Nullable AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        mContext = context;

    }
    /*####设置数据--需要实现*/

    /**
     * @param lowStrainDb 要显示的数据
     * @param isShowValid 要显示的数据有效性类型 0 -- 有效   1 -- 无效
     */
    public void setLowStrainDb(LowStrainDb lowStrainDb, boolean isShowValid) {
        this.isShowValid = isShowValid;
        if (isShowValid) {//需要的是 减号
            changeValidBitmap = BitmapFactory.decodeResource(mContext.getResources(), R.mipmap.low_strain_db_view_remove);
        } else {//需要的是加号
            changeValidBitmap = BitmapFactory.decodeResource(mContext.getResources(), R.mipmap.low_strain_db_view_add);
        }

        //赋值基本信息数据
        this.mLowStrainDb = lowStrainDb;
        //筛查需要显示的数据--根据有效性
        initSatisfactoryLowStrainDataDb();

        //布局管理器
        mLayoutManager = new LinearLayoutManager(this.mContext);
        setLayoutManager(mLayoutManager);
        //设置item等高
        setHasFixedSize(true);
        //设置间隔
        addItemDecoration(new SpaceItemDecoration(30));
        //设置适配器
        mAdapter = new LowStrainDbAdapter(mLowStrainDb, this.mContext);
        setAdapter(mAdapter);
        mAdapter.notifyDataSetChanged();
    }



    /*####设置列表的监听接口--需要实现*/
    public void setOnItemValidChangedListener(OnItemValidChangedListener listener) {
        this.mListener = listener;
    }

    /*设置编辑模式,有PATTERN_NORMAL 和 PATTERN_EDIT两种模式*/
    public void setRecycleViewPattern(byte pattern) {
        if (pattern == PATTERN_EDIT) {
            CURRENT_PATTERN = PATTERN_EDIT;
        } else {
            CURRENT_PATTERN = PATTERN_NORMAL;
        }
        if (mAdapter != null) {
            mAdapter.notifyDataSetChanged();
        }
        if (mAdapter != null) {
            setAdapter(mAdapter);
        }
    }

    /*获取当前视图的模式*/
    public byte getRecycleViewPattern() {
        return CURRENT_PATTERN;
    }

    /*####设置列表的显示模式--需要实现*/

    //添加符合要求的数据
    private void initSatisfactoryLowStrainDataDb() {
        mLowStrainDataDbList = new ArrayList<>();
        for (int i = 0; i < mLowStrainDb.lowStrainDataDb.size(); i++) {
            if ((mLowStrainDb.lowStrainDataDb.get(i).isValid == 0) == isShowValid) {
                mLowStrainDataDbList.add(mLowStrainDb.lowStrainDataDb.get(i));
            }
        }
    }

    //列表适配器
    class LowStrainDbAdapter extends Adapter<MyViewHolder> {
        private LowStrainDb mLowStrainDb;
        private Context mContext;

        public LowStrainDbAdapter(LowStrainDb lowStrainDb, Context mcontext) {
            this.mLowStrainDb = lowStrainDb;
            this.mContext = mcontext;
        }

        @Override
        public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(this.mContext).inflate(R.layout.item_layout_low_strain_db, parent, false);
            return new MyViewHolder(view);
        }

        @Override
        public void onBindViewHolder(final MyViewHolder holder, int position) {
            final int index = holder.getAdapterPosition();
            LowStrainDataDbView lowStrainDataDbView = new LowStrainDataDbView(this.mContext);
            lowStrainDataDbView.setLowStrainDataDb(this.mLowStrainDb, mLowStrainDataDbList, index);
            holder.ll_item_layout_low_strain_db.removeAllViews();
            holder.ll_item_layout_low_strain_db.addView(lowStrainDataDbView);
            //由当前的模式决定是否显示编辑按钮
            if (CURRENT_PATTERN == PATTERN_NORMAL) {
                //正常模式--按钮不显示
                holder.ll_item_layout_low_strain_edit.setVisibility(View.GONE);
            } else {
                //编辑模式--可见,有点击事件
                holder.ll_item_layout_low_strain_edit.setImageBitmap(changeValidBitmap);
                holder.ll_item_layout_low_strain_edit.setVisibility(View.VISIBLE);
                holder.ll_item_layout_low_strain_edit.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (mListener != null) {
                            mListener.onChanged(mLowStrainDataDbList.get(holder.getAdapterPosition()).uuid, !isShowValid);
                        }
                        mLowStrainDataDbList.remove(holder.getAdapterPosition());
                        setAdapter(mAdapter);
                    }
                });
            }
        }


        @Override
        public int getItemCount() {
            return mLowStrainDataDbList.size();
        }

    }

    /*控件容器*/
    class MyViewHolder extends ViewHolder {
        LinearLayout ll_item_layout_low_strain_db;//自定义视图的布局容器
        ImageView ll_item_layout_low_strain_edit;//编辑按钮

        public MyViewHolder(View itemView) {
            super(itemView);
            ll_item_layout_low_strain_db = (LinearLayout) itemView.findViewById(R.id.ll_item_layout_low_strain_db);
            ll_item_layout_low_strain_edit = (ImageView) itemView.findViewById(R.id.ll_item_layout_low_strain_edit);
        }
    }

//    class ImageViewClickListener implements OnClickListener {
//        private int index;
//        private ArrayList<LowStrainDb.LowStrainDataDb> lowStrainDataDbs;
//        private LowStrainDbAdapter adapter;
//
//        public ImageViewClickListener(int index, ArrayList<LowStrainDb.LowStrainDataDb> lowStrainDataDbs,LowStrainDbAdapter dbAdapter) {
//            this.index = index;
//            this.lowStrainDataDbs = lowStrainDataDbs;
//            this.adapter = dbAdapter;
//        }
//
//        @Override
//        public void onClick(View v) {
//            Log.i("onBindViewHolder点击事件", "--点击的锤序:" + lowStrainDataDbs.get(index).curNumber + ",有效性变为:" + !isShowValid);
//            if (mListener != null) {
//                mListener.onChanged(mLowStrainDataDbList.get(index).curNumber + "", !isShowValid);
//                Log.i("onBindViewHolder点击事件", "--点击的锤序:" + mLowStrainDataDbList.get(index).curNumber + ",有效性变为:" + !isShowValid);
//            }
//            mLowStrainDataDbList.remove(index);
//            adapter.notifyDataSetChanged();
//        }
//    }

    /*数据有效性改变监听接口*/
    public interface OnItemValidChangedListener {
        void onChanged(String uuid, boolean isValid);
    }


    /*列表子项绘图视图View*/

    /**
     * 实现的功能：
     * 1.绘制波形图
     * 2.判定事件引起的按钮改变（状态可随时改变）
     */
    public class LowStrainDataDbView extends View {
        private final String TAG = "LowStrainDataDbView";
        private LowStrainDb mLowStrainDb;
        private LowStrainDb.LowStrainDataDb mLowStrainDataDb;
        private float SCREEN_WIDTH = 0;
        private float SCREEN_HEIGHT = 0;
        /*红绿白灰*/
        private final int COLOR_LGRAY = Color.parseColor("#C9C9C9");
        private final int COLOR_THEME = Color.parseColor("#01FF05");

        private Paint mPaint = null;
        private Path mPath = null;
        private final int RADIUS_SIZE = 10;//圆角弧度大小
        private float TEXT_SIZE = 40;//绘制的文字大小
        private long mFingerDownTime = 0;
        private long mFingerUpTime = 0;
        private final long PRESS_TIME = 200;//毫秒
        private float DELTA_X = 0;
        private float DELTA_Y = 0;
        private float MAX_Y = 0;//Y轴最大数值
        private final int GAP_BORDER = 10;//上下边预留
        private float TITTLE_GAP = 0;
        private float START_INDEX = -1;
        private float END_INDEX = -1;

        public LowStrainDataDbView(Context context) {
            super(context);
            mPaint = new Paint();
            mPath = new Path();
            mPaint.setAntiAlias(true);
        }

        //设置绘制的对象
        public void setLowStrainDataDb(LowStrainDb lowStrainDb, ArrayList<LowStrainDb.LowStrainDataDb> lowStrainDataDbs, int index) {
            this.mLowStrainDb = lowStrainDb;
            this.mLowStrainDataDb = lowStrainDataDbs.get(index);
            invalidate();
        }

        @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            setMeasuredDimension(widthMeasureSpec, heightMeasureSpec);
            //获取控件的宽和高
            SCREEN_WIDTH = getMeasuredWidth();
            SCREEN_HEIGHT = getMeasuredHeight();

            if (SCREEN_WIDTH < SCREEN_HEIGHT) {
                float temp = SCREEN_WIDTH;
                SCREEN_WIDTH = SCREEN_HEIGHT;
                SCREEN_HEIGHT = temp;
            }
            TITTLE_GAP = SCREEN_HEIGHT / 5;

            //防止文字过大 导致显示残缺
            if (TEXT_SIZE > ((TITTLE_GAP - 10) / 2)) {
                TEXT_SIZE = (TITTLE_GAP - 10) / 2;
            }

//            desRect = new Rect(Math.round(SCREEN_WIDTH - SCREEN_HEIGHT / 5 + SCREEN_HEIGHT / 20), Math.round(SCREEN_HEIGHT / 20), Math.round(SCREEN_WIDTH - SCREEN_HEIGHT / 20), Math.round(3 * SCREEN_HEIGHT / 20));

        }

        @Override
        protected void onDraw(Canvas canvas) {
            //绘制底色
            canvas.drawColor(Color.BLACK);
            mPaint.setStrokeWidth(1);

            mPaint.setStyle(Paint.Style.STROKE);
            mPaint.setColor(COLOR_LGRAY);

            if (this.mLowStrainDataDb == null) {//数据为空
                drawException(canvas, "该项数据为空");
                Log.e(TAG + "--错误", "该View中的LowStrainDataDb对象为null,无法绘制波形");
                return;

            }
            else
            {
                //标题线
                mPaint.setStyle(Paint.Style.FILL);
                mPaint.setTextSize(TEXT_SIZE);
                mPaint.setTextAlign(Paint.Align.LEFT);
                mPaint.setColor(COLOR_LGRAY);
                canvas.drawLine(0, TITTLE_GAP, SCREEN_WIDTH, TITTLE_GAP, mPaint);

                //当前锤序
                mPaint.setColor(Color.WHITE);
                mPaint.setTextAlign(Paint.Align.LEFT);
                canvas.drawText("锤序:" + this.mLowStrainDataDb.curNumber, 10, TITTLE_GAP / 2 + TEXT_SIZE / 2, mPaint);

//                //参数信息
//                String pileNo = "桩号:" + mLowStrainDb.pileNo;//桩号
//                String pileLength = "桩长:" + mLowStrainDb.pileLength + "m";//桩长
//                String velocity = "波速:" + mLowStrainDb.velocity + "m/s"; //波速,单位:m/s
//                String pileDiameter = null;//保留两位小数
//                try {
//                    pileDiameter = "桩径:" + String.format("%.2fmm", mLowStrainDb.pileDiameter);//桩径
//                } catch (Exception e) {
//                    e.printStackTrace();
//                    pileDiameter = "桩径:未知";
//                }
//
//
//                //绘制桩号
//                mPaint.setPathEffect(null);
//                mPaint.setTextSize(TEXT_SIZE);
//                mPaint.setColor(Color.BLACK);
//
//                canvas.drawText(pileNo, 0, TITTLE_GAP / 2 - 5, mPaint);
//
//                //绘制桩长
//                float leftGap = pileNo.length() > pileDiameter.length() ? (pileNo.length() + 0.5f) : (pileDiameter.length() + 0.5f);
//                canvas.drawText(pileLength, leftGap * TEXT_SIZE, TITTLE_GAP / 2 - 5, mPaint);
//
//                //绘制桩径
//                canvas.drawText(pileDiameter, 0, TITTLE_GAP - 5, mPaint);
//
//                //绘制波速
//                canvas.drawText(velocity, leftGap * TEXT_SIZE, TITTLE_GAP - 5, mPaint);
//
                //绘图数据mLowStrainDataDb.waveData

                if (this.mLowStrainDataDb.waveData == null) {
                    drawException(canvas, "数据序列为空");
                    Log.e(TAG, "该数据的waveData成员属性为null");
                    return;
                }

                Float data[] = this.mLowStrainDataDb.waveData;
                try {
                    handleDataArray(data);
                } catch (Exception e) {
                    e.printStackTrace();
                    Log.e(TAG, "该数据的采样间隔为0，无法计算桩底");
                }

                DELTA_X = SCREEN_WIDTH / data.length;
                DELTA_Y = (SCREEN_HEIGHT - TITTLE_GAP - GAP_BORDER) / (2 * MAX_Y);//预留5pixel

                //绘制中心线
                mPaint.setColor(Color.WHITE);
                mPaint.setPathEffect(new DashPathEffect(new float[]{5,5},0));
                setLayerType(LAYER_TYPE_SOFTWARE,null);
                canvas.drawLine(0, SCREEN_HEIGHT / 2 + TITTLE_GAP / 2, SCREEN_WIDTH, SCREEN_HEIGHT / 2 + TITTLE_GAP / 2, mPaint);

                //绘制桩顶桩底信息
                drawPileTopAndBottom(canvas);

                //设置画笔
                mPaint.setPathEffect(null);
                mPaint.setStyle(Paint.Style.STROKE);
                mPaint.setStrokeCap(Paint.Cap.ROUND);
                mPaint.setColor(Color.WHITE);
                mPaint.setStrokeWidth(2);

                //绘制波形
                mPath.reset();//重置mPath路径
                mPath.moveTo(0, SCREEN_HEIGHT / 2 + TITTLE_GAP / 2);
                mPaint.setStrokeWidth(4);
                mPaint.setColor(COLOR_THEME);

                for (int i = 0; i < data.length; i++) {
                    mPath.lineTo(DELTA_X * i, SCREEN_HEIGHT / 2 + TITTLE_GAP / 2 - (data[i] * DELTA_Y));
                }
                canvas.drawPath(mPath, mPaint);
//                Log.i(TAG, "--波形绘制完成");

            }

        }

        //出现异常时执行的代码
        private void drawException(Canvas canvas, String info) {
            canvas.drawColor(Color.WHITE);
            mPaint.setTextAlign(Paint.Align.CENTER);
            mPaint.setColor(COLOR_LGRAY);
            mPaint.setTextSize(TEXT_SIZE * 2);
            mPaint.setPathEffect(null);
            canvas.drawText(info, SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2, mPaint);
        }

        //绘制桩顶桩底信息
        private void drawPileTopAndBottom(Canvas canvas) {
            //桩顶竖直线
//            Log.i("START_INDEX", START_INDEX + "");
            mPaint.setPathEffect(new DashPathEffect(new float[]{5,5},0));
            if (START_INDEX != -1)
            {
                canvas.drawLine(START_INDEX * DELTA_X, TITTLE_GAP, START_INDEX * DELTA_X, SCREEN_HEIGHT, mPaint);
//                canvas.drawLine(SCREEN_WIDTH / 3, TITTLE_GAP, SCREEN_WIDTH / 3, SCREEN_HEIGHT, mPaint);//测试代码
            }

            //桩底竖直线
            if (END_INDEX != -1)
            {
                canvas.drawLine(END_INDEX * DELTA_X, TITTLE_GAP, END_INDEX * DELTA_X, SCREEN_HEIGHT, mPaint);
//              canvas.drawLine(SCREEN_WIDTH * 2 / 3, TITTLE_GAP, SCREEN_WIDTH * 2 / 3, SCREEN_HEIGHT, mPaint);//测试代码
            }

            //蓝色矩阵
            if (START_INDEX != -1 && END_INDEX != -1)
            {
                mPaint.setColor(Color.YELLOW);
                mPaint.setStyle(Paint.Style.FILL);
                canvas.drawRect(START_INDEX * DELTA_X, SCREEN_HEIGHT - 10, END_INDEX * DELTA_X, SCREEN_HEIGHT, mPaint);
            }
//            canvas.drawRect(SCREEN_WIDTH / 3, SCREEN_HEIGHT - 30, SCREEN_WIDTH * 2 / 3, SCREEN_HEIGHT, mPaint);//测试代码

            //标高
            mPaint.setColor(Color.WHITE);
            mPaint.setTextAlign(Paint.Align.LEFT);
            if (START_INDEX != -1) {
//                canvas.drawText("0m", SCREEN_WIDTH / 3 + 2, SCREEN_HEIGHT - 35, mPaint);//测试代码
                canvas.drawText("0m", START_INDEX * DELTA_X, SCREEN_HEIGHT - 35, mPaint);
            }
            if (END_INDEX != -1) {
//                canvas.drawText("15m", SCREEN_WIDTH * 2 / 3 + 2, SCREEN_HEIGHT - 35, mPaint);//测试代码
                canvas.drawText(mLowStrainDb.pileLength + "m", END_INDEX * DELTA_X, SCREEN_HEIGHT - 35, mPaint);
            }
        }

        //绘制按钮
//        private void drawButtuns(Canvas canvas) {
//            Log.i(TAG, "绘制有效性切换按钮中...");
//
////            canvas.drawBitmap(changeValidBitmap, bitmapRect, desRect, mPaint);
//
////            //绘制按钮背景
////            mPaint.setColor(COLOR_DGRAY);
////            mPaint.setStyle(Paint.Style.FILL);
////            canvas.drawRect(4 * SCREEN_WIDTH / 5, 0, SCREEN_WIDTH, SCREEN_HEIGHT / 5, mPaint);
////
////            if (mCurrentDataValid) {//有效数据
////                //左半部分
////                mPaint.setColor(COLOR_GREEN);
////                canvas.drawRect(4 * SCREEN_WIDTH / 5, 0, 9 * SCREEN_WIDTH / 10, SCREEN_HEIGHT / 5, mPaint);
////                mPaint.setColor(COLOR_WHITE);
////                mPaint.setTextSize(SCREEN_WIDTH / 22);
////                mPaint.setTextAlign(Paint.Align.CENTER);
////                canvas.drawText("有效", 17 * SCREEN_WIDTH / 20, 3 * SCREEN_HEIGHT / 20, mPaint);
////                //右半部分
////                mPaint.setColor(COLOR_LGRAY);
////                canvas.drawText("无效", 19 * SCREEN_WIDTH / 20, 3 * SCREEN_HEIGHT / 20, mPaint);
////
////            } else {//无效数据
////                //左半部分
////                mPaint.setColor(COLOR_LGRAY);
////                mPaint.setTextSize(SCREEN_WIDTH / 22);
////                mPaint.setTextAlign(Paint.Align.CENTER);
////                canvas.drawText("有效", 17 * SCREEN_WIDTH / 20, 3 * SCREEN_HEIGHT / 20, mPaint);
////                //右半部分
////                mPaint.setColor(COLOR_RED);
////                canvas.drawRect(9 * SCREEN_WIDTH / 10, 0, SCREEN_WIDTH, SCREEN_HEIGHT / 5, mPaint);
////                mPaint.setColor(COLOR_WHITE);
////                canvas.drawText("无效", 19 * SCREEN_WIDTH / 20, 3 * SCREEN_HEIGHT / 20, mPaint);
////
////            }
//
//        }

        /**
         * 如果点击的是两个判定按钮 那么事件不向上传递
         * 否则传递
         *
         * @param event 界面触碰事件 触发监听事件
         * @return
         */
        @Override
        public boolean onTouchEvent(MotionEvent event) {
            //由于增加了侧边按钮，所以屏蔽触控事件
            return true;
//            //如果LowStrainDataDb为空 则屏蔽触控事件
//            if (mLowStrainDataDb == null || mLowStrainDb == null) {
//                return true;
//            }
//
//            switch (event.getAction()) {
//                case MotionEvent.ACTION_DOWN:
//                    mFingerDownTime = System.currentTimeMillis();
//                    Log.i("手指按下的时间", String.valueOf(mFingerDownTime));
//                    return true;
//                case MotionEvent.ACTION_UP:
//                    mFingerUpTime = System.currentTimeMillis();
//                    //点击范围在按钮之内 且 时间符合要求(200ms之内)
//                    if (desRect.contains(Math.round(event.getX()), Math.round(event.getY())) && ((mFingerUpTime - mFingerDownTime) <= PRESS_TIME)) {
//                        if (mListener != null) {
//                            mListener.onChangedListener(!isShowValid);
//                        }
//                        Log.i("手指放起的时间", String.valueOf(mFingerUpTime));
//                    }
//                    invalidate();
//                    break;
//                default:
//                    break;
//            }
//
//            return super.onTouchEvent(event);
        }


        /**
         * 积分 低应变处理
         *
         * @param data 输入波形
         * @return 处理后的数组
         */
        private void handleDataArray(Float[] data) throws Exception
        {
            MAX_Y = data[0];
            for (int i = 1; i < data.length; i++) {
//                MAX_Y = (MAX_Y < Math.abs(data[i])) ? Math.abs(data[i]) : MAX_Y;
                if (MAX_Y <= Math.abs(data[i])) {
                    MAX_Y = Math.abs(data[i]);
                    START_INDEX = i;//确定桩顶位置
                }
            }

            //确定桩底位置
            float bottomIndex = START_INDEX + (2 * mLowStrainDb.pileLength * 1000000 / mLowStrainDb.velocity) / mLowStrainDataDb.samplingInterval;
            END_INDEX = Math.round(bottomIndex);
            if (END_INDEX <= START_INDEX)
            {
                END_INDEX = -1;
            }
        }
    }


    //用来给RecycleView设置item间隔
    class SpaceItemDecoration extends ItemDecoration {

        int mSpace;

        /**
         * @param outRect Rect to receive the output.
         * @param view    The child view to decorate
         * @param parent  RecyclerView this ItemDecoration is decorating
         * @param state   The current state of RecyclerView.
         */

        @Override

        public void getItemOffsets(Rect outRect, View view, RecyclerView parent, State state) {

            super.getItemOffsets(outRect, view, parent, state);
//            outRect.left = 0;
//            outRect.right = 0;

            outRect.bottom = mSpace;

//            if (parent.getChildAdapterPosition(view) == 0) {
//                outRect.top = mSpace;
//            }

        }


        public SpaceItemDecoration(int space) {
            this.mSpace = space;

        }

    }
}